package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.service.ShopCarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShopCarService {
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //添加购物车
    public void addShoppingCar(ShoppingCartDTO shoppingCartDTO) {
        //确定商品是否在购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long id = BaseContext.getCurrentId();//获得用户id
        shoppingCart.setUserId(id);
        List<ShoppingCart> list = shoppingCarMapper.getShoppingCar(shoppingCart);//查询购物车数据
        //如果存在则数量加1
        if(list != null && list.size() > 0){
            //获取第一个数据
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            //更新数据相应的数量
            shoppingCarMapper.updateNumberById(cart);
        }else {
            //否则添加数据
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if(dishId != null){//添加的是菜品
                //获取菜品的名称、图片路径、商品单价
                List<Long> ids = new ArrayList<>();
                ids.add(dishId);
                List<Dish> dishes = dishMapper.getStatusByIds(ids);
                shoppingCart.setName(dishes.get(0).getName());
                shoppingCart.setImage(dishes.get(0).getImage());
                shoppingCart.setAmount(dishes.get(0).getPrice());
            }else {
                //获取套餐的名称、图片、商品单价
                List<Long> ids = new ArrayList<>();
                ids.add(setmealId);
                List<Setmeal> setmeals = setmealMapper.getByIds(ids);
                shoppingCart.setName(setmeals.get(0).getName());
                shoppingCart.setImage(setmeals.get(0).getImage());
                shoppingCart.setAmount(setmeals.get(0).getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加数据
            shoppingCarMapper.addShoppngCar(shoppingCart);
        }
    }

    //查询购物车
    public List<ShoppingCart> showShoppingCar() {
        //获取当前用户id
        Long id = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(id)
                .build();
        List<ShoppingCart> list = shoppingCarMapper.getShoppingCar(shoppingCart);
        return list;
    }

    //清空购物车
    public void cleanShoppingCar() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCarMapper.cleanShoppingCar(userId);
    }

    //删除一个商品
    public void subShoppingCar(ShoppingCartDTO shoppingCartDTO) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        cart.setUserId(userId);
        //查询符合条件的购物车数据
        List<ShoppingCart> list = shoppingCarMapper.getShoppingCar(cart);
        if(list != null && list.size() > 0){
            ShoppingCart cart1 = list.get(0);
            int number = (int)cart1.getNumber();
            if( number == 1){//直接删除该记录
                shoppingCarMapper.deleteById(cart1.getId());
            }else {//数量减一
                cart1.setNumber(cart1.getNumber() -1);
                shoppingCarMapper.updateNumberById(cart1);
            }
        }
    }
}
